home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_075 / comm / beep.c next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  112 lines

  1.  
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <devices/audio.h>
  5. #include <fcntl.h>
  6. #include "globals.h"
  7.  
  8. #define LEFT0  1
  9. #define RIGHT0 2
  10. #define LEFT1  4
  11. #define RIGHT1 8
  12.  
  13. #ifdef  TRUE
  14. #undef  TRUE
  15. #undef  FALSE
  16. #endif
  17.  
  18. #define TRUE   1
  19. #define FALSE  0
  20.  
  21. extern struct   MsgPort *CreatePort();
  22. extern UBYTE    *AllocMem();
  23. static UBYTE allocationMap[] = { LEFT0, LEFT1, RIGHT0, RIGHT1 };
  24. static UBYTE *beepdata = NULL, *oldbeep = NULL, sdata;
  25. static short beepinit  = FALSE;
  26. static struct IOAudio *ioa = NULL;          /* Audio IO control block */
  27.  
  28.  
  29. InitBeep()
  30. {
  31. /* allocate memory for Audio IO control block pointer -- longword aligned */
  32.    if((ioa = (struct IOAudio *)AllocMem((long)sizeof(struct IOAudio),
  33.                  MEMF_CHIP | MEMF_CLEAR)) == NULL)
  34.       return FALSE;
  35.  
  36.    if((beepdata =(UBYTE *)AllocMem(BEEPSIZE,MEMF_CHIP | MEMF_CLEAR)) == NULL)
  37.    {
  38.       FreeMem(ioa,(long)sizeof(struct IOAudio));
  39.       ioa = NULL;
  40.       return FALSE;
  41.    }
  42.    beepdata[ 0 ] = 100;
  43.  
  44. /* open the audio device */
  45.    ioa->ioa_Request.io_Message.mn_Node.ln_Pri  = 10;   /* any channel */
  46.    ioa->ioa_Request.io_Message.mn_ReplyPort    = CreatePort("Comm.snd",0L);
  47.    if(ioa->ioa_Request.io_Message.mn_ReplyPort == 0)
  48.    {
  49.       FreeMem(ioa,(long)sizeof(struct IOAudio));
  50.       FreeMem(beepdata,BEEPSIZE);
  51.       ioa = NULL;
  52.       return FALSE;
  53.    }
  54.  
  55.    ioa->ioa_Data   = allocationMap;
  56.    ioa->ioa_Length = sizeof(allocationMap);
  57.    if(OpenDevice(AUDIONAME,0L,ioa,0L))
  58.    {
  59.       FreeMem(ioa,(long)sizeof(struct IOAudio));
  60.       FreeMem(beepdata,BEEPSIZE);
  61.       DeletePort((long)ioa->ioa_Request.io_Message.mn_ReplyPort);
  62.       ioa = NULL;
  63.       return FALSE;
  64.    }
  65.    ioa->ioa_Request.io_Command = CMD_WRITE;
  66.    ioa->ioa_Request.io_Flags = ADIOF_PERVOL;
  67.    ioa->ioa_Period = install.period;
  68.    ioa->ioa_Volume = install.volume;
  69.    ioa->ioa_Cycles = install.cycles;
  70.    ioa->ioa_Length = install.length;
  71.    ioa->ioa_Data   = beepdata;
  72.  
  73.    return TRUE;
  74. }
  75.  
  76. /*************
  77.     clean up
  78. *************/
  79. Close_Beep()
  80. {
  81.    if(ioa)                       /* if memory allocated */
  82.    {
  83.       if(beepinit && CheckIO(ioa) == FALSE)
  84.           WaitIO(ioa);
  85.       CloseDevice(ioa);
  86.       if(ioa->ioa_Request.io_Message.mn_ReplyPort)
  87.          DeletePort((long)ioa->ioa_Request.io_Message.mn_ReplyPort);
  88.       FreeMem(ioa->ioa_Data,ioa->ioa_Length);
  89.       FreeMem(ioa,(long)sizeof(struct IOAudio));
  90.    }
  91. }
  92.  
  93.  
  94. Beep()
  95. {
  96.    if(ioa == NULL)  return FALSE;
  97.  
  98.    ioa->ioa_Request.io_Command = CMD_WRITE;
  99.    ioa->ioa_Request.io_Flags   = ADIOF_PERVOL;
  100.  
  101.    if(beepinit)
  102.    {
  103.      if(CheckIO(ioa) == FALSE)
  104.        return TRUE;
  105.      WaitIO(ioa);
  106.    }
  107.    BeginIO(ioa);             /* play it SAM... */
  108.    beepinit = TRUE;
  109.    return TRUE;
  110. }
  111.  
  112.